#!/usr/local/bin/env python3
#coding=utf-8
import argparse

import matplotlib.pylab as plt 
import matplotlib
matplotlib.style.use('ggplot')

from config import *
from data_net import HTTP,para_report,get_report_info
from statistic import get_all_method, DealData
import os
import time 

def get_arguments():
    desc = """
    this is a client or Api for search/statistic weather information from http://datacenter.mep.gov.cn 
        written by qingluan
    """

    parser = argparse.ArgumentParser(usage="how to use this",description=desc)
    parser.add_argument("-d",'--download',default=False,action="store_true",help=" donwload mode ")
    parser.add_argument("-c",'--city',default="",help="donwload/statistic mode set city : \"-c  \'北京市\' \"")
    parser.add_argument("-t",'--date',default="2015-1-1 2015-12-24",help="donwload mode set date from start to end : \"-t \'2015-1-1 2015-12-24\' \"")
    parser.add_argument("-f",'--save-file',default=False,action="store_true",help="donwload mode set date from start to end : \"-t \'2015-1-1 2015-12-24\' \"")

    parser.add_argument("-s","--statistic",default=False,action="store_true",help="statistic information mode")
    parser.add_argument("-w","--way",default="line",help="which Figure to show [line/bar/hist/kde/pie] ")
    parser.add_argument("-a","--all",default=False,action="store_true",help="list all method to statistic")
    parser.add_argument("--search-city",default="",help="search a city 's data if donwload in locality ")
    parser.add_argument("--args",default="",help="args for way method  \"-w \'classfy_air_rank\' --args \'pie\' \" ")
    return parser.parse_args()




if __name__ == "__main__":
    args = get_arguments()
    citys = [quote(city) for city in args.city.strip().split()]
    # if len(citys)  == 1:
    city = ""
    if len(citys) >= 1:
        city = citys[0]

    date = args.date.split()

    
    if not os.path.exists(BASE_LOCAL_PATH) :
        os.mkdir(BASE_LOCAL_PATH)

    if args.download:
        for city in citys:
            first_url =  config_paras(city, date, 1)
            client = HTTP()
            client.set_save(args.save_file)
            client.Get(first_url, get_report_info)

            time.sleep(4)
            pages_len = client.get_pages()
            urls = [config_paras(city,date,1) ]
            if pages_len > 1:
                urls = [config_paras(city,date,i) for i in range(1, client.get_pages()+1)]
            

        # print(url)

            for url in urls:
                client.Get(url, para_report)

    if args.search_city:
        print ([ i for i in os.listdir(BASE_LOCAL_PATH) if args.search_city in i ])

    if args.all:
        print("you can use method : ")
        print(get_all_method(DealData))

    if args.statistic:
        print(" --- plot ---")
        if args.city == "":
            print("you must point a city in local res file ")
        Deal = DealData(args.city)
        if hasattr(Deal, args.way): 
            print("get it ",args.way)
            method = getattr(Deal, args.way)
            args_method = args.args.split()
            method(*args_method)
            plt.show()



    
